#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#include <string.h>
#include <pthread.h>

#define  BUFSIZE            (100)
#define  ADRESS             "127.0.0.1"
#define  PORT               (9734)
#define  OUT
#define  ARGUM               (3)


static void* client1(void* arg);
static void* client2(void* arg);

static int SocketNew(int* _socket,struct sockaddr_in* _saddr,const char* _name)
{
    *_socket = socket( AF_INET /*1st argument is the purpose AF_UNIX local*/ ,
                SOCK_STREAM /*communication sematics SOCK_STREAM ==two way connect*/,
                0/*Protocol*/);

    if(*_socket ==-1)
    {
        perror("socket");
        return 1;
    }
    _saddr->sin_family = AF_INET;/*dueto AF_UNIX unix(7) instructions*/
    _saddr->sin_addr.s_addr = inet_addr(ADRESS);/*dueto AF_UNIX unix(7) instructions)*/
    _saddr->sin_port = htons(PORT);/*convert byte representation on number to network represent*/
    return 0;
}

/*Add to the massage Header at sizeof int + 1 and send to server*/
static int AddHeader(char* buf1,char* buf2)
{
    *(int*)buf2 = strlen(buf1)+1;
    strcat((buf2 + sizeof(int)),buf1);
    return  *(int*)buf2 + sizeof(int);
}

int main()
{

    pthread_t           thread[ARGUM ];

    pthread_create( &thread[0], NULL, client1, 0);
    pthread_create( &thread[1], NULL, client2, 0);

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);

  return 0;
}

static void* client1(void* arg)
{
    int                 client_sockfd;
    struct sockaddr_in  client_saddr;
    char*               mes1="Message 1";
    int                 err;
    int                 i=1;
    int                 ln,wr;
    char                headbuffer[BUFSIZE];

    if(SocketNew(&client_sockfd,&client_saddr,0) )
    {
        fprintf(stderr,"\nSocket New problems");
        exit(EXIT_FAILURE);
    }

    err = connect(client_sockfd,(struct sockaddr*)&client_saddr,sizeof(client_saddr));
    if(err == -1)
    {
        perror("connect");
        fflush(stdout);
        exit(EXIT_FAILURE);
    }
/*       close(err);*/

    while(1)
    {
        memset(headbuffer,0,sizeof(headbuffer));
        ln=AddHeader(mes1,OUT headbuffer);
        wr =0;
/*        wr= write(client_sockfd,headbuffer,ln);
        printf("\n Send message N %d : \" %s \" lenght %d ",i,headbuffer+sizeof(int),wr);*/
        sleep(3);

        if(wr == -1 && errno == EPIPE)
        {
            perror(" mes1 send :    ");
            break;
        }
        ++i;
    }
  close(client_sockfd);
  printf("\nClient 1 finished\n");
  return (void*)0;
}

static void* client2(void* arg)
{
    int                 client_sockfd;
    struct sockaddr_in  client_saddr;
    char*               mes2="Message 2";
    int                 err;
    int                 i=1;
    int                 ln,wr;
    char                headbuffer[BUFSIZE];
    if(SocketNew(&client_sockfd,&client_saddr,0) )
    {
        fprintf(stderr,"\nSocket New problems");
        exit(EXIT_FAILURE);
    }

    err = connect(client_sockfd,(struct sockaddr*)&client_saddr,sizeof(client_saddr));
    if(err == -1)
    {
        perror("connect");
        fflush(stdout);
        exit(EXIT_FAILURE);
    }

    while(1)
    {
        memset(headbuffer,0,sizeof(headbuffer));
        ln=AddHeader(mes2,OUT headbuffer);
        wr =0;
/*        wr= write(client_sockfd,headbuffer,ln);*
        printf("\n Send message N %d : \" %s \" lenght %d ",i,headbuffer+sizeof(int),wr);*/
        sleep(2);
        ++i;
        if(wr == -1 && errno == EPIPE)
        {
            perror(" mes2 send :    ");
            break;
        }
    }
  close(client_sockfd);
printf("\nClient 2 finished\n");
  return (void*) 0;
}




